﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;

namespace COER
{
    public partial class CopyDailog : Form
    {
        public CopyDailog()
        {
            InitializeComponent();

            GetControls(this.Controls);
        }

        private void GetControls(Control.ControlCollection ctrls)
        {
            foreach (Control item in ctrls)
            {
                if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((Button)item).Click += new EventHandler(this.btn_Switch_Click);
                }

                if (item is DLLPC.PCAP.PCButton && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((DLLPC.PCAP.PCButton)item).OP = Kernel.OP;
                }

                if (item.Controls != null)
                {
                    GetControls(item.Controls);
                }
            }
        }

        public void ShowForm(List<string> vRecs)
        {
            cbxRecipe.Items.Clear();
            tbxNewRec.Text = "";

            foreach (var item in vRecs)
            {
                cbxRecipe.Items.Add(item);
            }

            if (cbxRecipe.Items.Count > 0)
            {
                cbxRecipe.SelectedIndex = 0;
            }

            btnSave.Text = ckbCopy.Checked == true ? "复制" : "新建";
            IsCanAdd = false;
            Dr = DialogResult.No;
            this.ShowDialog();
        }

        public DialogResult Dr { get; set; } = DialogResult.No;
        public bool IsCanAdd { get; set; } = false;
        public string NewName { get; set; } = "";
        public string SrcName { get; set; } = "";
        public bool IsCopy { get; set; } = true;

        private string sourceRecName = "";

        private void btn_Switch_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string[] info = btn.Tag.ToString().Split('|');

            switch (info[1])
            {
                case "Save":
                    {
                        NewName = tbxNewRec.Text.Trim();

                        if (string.IsNullOrWhiteSpace(NewName))
                        {
                            MessageBox.Show("请输入工单名称");
                            return;
                        }

                        if (cbxRecipe.Items.Contains(NewName))
                        {
                            MessageBox.Show("工单名称已经存在，无法重复添加");
                            return;
                        }

                        if (ckbCopy.Checked == true)
                        {
                            IsCopy = true;
                            sourceRecName = cbxRecipe.SelectedItem.ToString();
                        }
                        else
                        {
                            IsCopy = false;
                            sourceRecName = "ABC";
                        }

                        Dr = DialogResult.Yes;
                    }
                    break;
                case "Exit":
                    {
                        Dr = DialogResult.No;
                    }
                    break;
            }

            this.Visible = false;
        }

        private void ckbCopy_CheckedChanged(object sender, EventArgs e)
        {
            IsCopy = ckbCopy.Checked;

            btnSave.Text = ckbCopy.Checked == true ? "复制" : "新建";
        }

        private void cbxRecipe_SelectedIndexChanged(object sender, EventArgs e)
        {
            SrcName = cbxRecipe.SelectedItem.ToString();
        }
    }
}
